package com.fk.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fk.bean.Question;
import com.fk.regimen.R;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class QuestItemAdapter extends BaseAdapter {

	//接收到的数据集合
	List<Question> list;
	//上下文参数：表示哪个界面想调用此适配器
	Context context;
	//LayoutInflater：专门用来初始化视图
	LayoutInflater mInflater;
	
	
	String tag = "china";
	
	public QuestItemAdapter(List<Question> list, Context context){
		super();
		this.list = list;
		this.context = context;
		mInflater = LayoutInflater.from(context);
	}
	
	
	@Override
	public int getCount() {
		Log.e(tag, "getCount --> " + list.size());
		// 返回数据总数
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		Log.e(tag, "getItem --> " + position);
		// 返回每一行绑定的数据，对于本例就是，就是DataBean对象
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		Log.e(tag, "getItemId --> " + position);
		//返回每一行的id，很少用,positon表示当前行的索引，从0开始
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		Log.e(tag, "getView --> " + position);
		final Map<Integer,Integer> checkedMap = new HashMap<Integer,Integer>();
		//声明viewholder对象
		final ViewHolder viewHolder;
		
		//1、指定每一行的布局；2、指定每一行布局绑定的数据
		//convertView：表示每一行返回的布局
		//加载第一行数据时，convertView为空，从第二行开始，都使用第一行指定的布局
		if(convertView == null){
			convertView = mInflater.inflate(R.layout.phytest_listview, null);
			viewHolder = new ViewHolder();
			//指定每一行布局的具体图片或文本
			viewHolder.tv_questdescrib = (TextView) convertView.findViewById(R.id.tv_questdescrib);
			viewHolder.rg_question = (RadioGroup) convertView.findViewById(R.id.rg_question);
			
			viewHolder.rb_answerA = (RadioButton) convertView.findViewById(R.id.rb_answerA);
			viewHolder.rb_answerB = (RadioButton) convertView.findViewById(R.id.rb_answerB);
			viewHolder.rb_answerC = (RadioButton) convertView.findViewById(R.id.rb_answerC);
			viewHolder.rb_answerD = (RadioButton) convertView.findViewById(R.id.rb_answerD);
			viewHolder.rb_answerE = (RadioButton) convertView.findViewById(R.id.rb_answerE);
			
			convertView.setTag(viewHolder);
			
		}else{
			viewHolder = (ViewHolder) convertView.getTag();
		}
		
		Log.e(tag, "问题是？？？"+list.get(position).getQdescription());
		viewHolder.tv_questdescrib.setText(list.get(position).getQdescription());
		viewHolder.rb_answerA.setText("没有");
		viewHolder.rb_answerB.setText("很少");
		viewHolder.rb_answerC.setText("有时");
		viewHolder.rb_answerD.setText("经常");
		viewHolder.rb_answerE.setText("总是");
		
		
		//先复位
		viewHolder.rg_question.clearCheck();
		
		//看看这道题曾经有没有选过，如果选过就将对应的选项置为选中状态
		if(checkedMap.containsKey(position)){
			RadioButton rb = (RadioButton) viewHolder.rg_question.getChildAt(checkedMap.get(position));
//			if(rb != null){
				rb.setChecked(true);
//			}
		}
		
		//因为radioGroup的onCheckedChangListenner太坑了，不用它。用每个的点击事件好了
		viewHolder.rb_answerA.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				checkedMap.put(position, 0);
			}
		});
		viewHolder.rb_answerB.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				checkedMap.put(position, 1);
			}
		});
		viewHolder.rb_answerC.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				checkedMap.put(position, 2);
			}
		});
		viewHolder.rb_answerD.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				checkedMap.put(position, 3);
			}
		});
		viewHolder.rb_answerE.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				checkedMap.put(position, 4);
			}
		});
		
		
		
		viewHolder.rg_question.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				resetStatus(viewHolder);
				
				if(checkedId == viewHolder.rb_answerA.getId()){
					viewHolder.rb_answerA.setTextColor(Color.rgb(255, 255, 255));
				}else if(checkedId == viewHolder.rb_answerB.getId()){
					viewHolder.rb_answerB.setTextColor(Color.rgb(255, 255, 255));
				}else if(checkedId == viewHolder.rb_answerC.getId()){
					viewHolder.rb_answerC.setTextColor(Color.rgb(255, 255, 255));
				}else if(checkedId == viewHolder.rb_answerD.getId()){
					viewHolder.rb_answerD.setTextColor(Color.rgb(255, 255, 255));
				}else if(checkedId == viewHolder.rb_answerE.getId()){
					viewHolder.rb_answerE.setTextColor(Color.rgb(255, 255, 255));
				}
				
			}
		});
		
		
		return convertView;
	}
	
	
	
	public void resetStatus(ViewHolder viewHolder){
		viewHolder.rb_answerA.setTextColor(Color.rgb(0, 0, 0));
		viewHolder.rb_answerB.setTextColor(Color.rgb(0, 0, 0));
		viewHolder.rb_answerC.setTextColor(Color.rgb(0, 0, 0));
		viewHolder.rb_answerD.setTextColor(Color.rgb(0, 0, 0));
		viewHolder.rb_answerE.setTextColor(Color.rgb(0, 0, 0));
	}
	
	
	
	//视图缓存优化
	class ViewHolder{
		TextView tv_questdescrib;
		RadioGroup rg_question;
		RadioButton rb_answerA;
		RadioButton rb_answerB;
		RadioButton rb_answerC;
		RadioButton rb_answerD;
		RadioButton rb_answerE;
		
		
	}
	

}
